package yuejian_07.yuejian_07.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.javabean.User;
import yuejian_07.yuejian_07.model.UserModel;

import static yuejian_07.yuejian_07.base.Constant.USER_PHOTO_URL;

/**
 * Created by dell on 2017/11/6.
 */

public class ModifyUserActivity extends AppCompatActivity {
    private EditText userphone;
    private ImageView left;
    private EditText username;
    private ImageView userpic;
    private EditText userpassword;
    private Button modify_button;
    private int user_id;
    private String user_name;
    private String user_password;
    private String  user_phone;
    private String  user_pic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_user);
        initView();
        getUserDate();
        initEvent();
    }
    //修改个人信息
    public void initView(){
        userphone=(EditText)findViewById(R.id.u_phone);
        username=(EditText)findViewById(R.id.u_name);
        userpassword=(EditText)findViewById(R.id.u_pass);
        modify_button=(Button)findViewById(R.id.modify_button);
        userpic=(ImageView)findViewById(R.id.u_pic);
        left = (ImageView) findViewById(R.id.left);
    }
    public void getUserDate(){
        SharedPreferences sp=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sp.getInt("user_id",0);
        // Log.d("userbg","u_id");

        UserModel userModel = new UserModel();
        Call call = userModel.edituser(user_id);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();
                username.setText(user.getUser_name());
                userpassword.setText(user.getUser_password());
                userphone.setText(user.getUser_phone());
                // Log.d("userbg","username");
                String upic=USER_PHOTO_URL+user.getUser_pic();
                Picasso.with(ModifyUserActivity.this).load(upic).into(userpic);
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
            }
        });
    }
    public void initEvent(){
//        left.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View view) {
//                finish();
//            }
//        });

        modify_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                modifyDate();
            }
        });
        left.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void modifyDate() {
        user_name = username.getText().toString();
        user_password = userpassword.getText().toString();
        user_phone = userphone.getText().toString();


        if (user_name.equals("")) {
            Toast.makeText(ModifyUserActivity.this, "用户名为空", Toast.LENGTH_SHORT).show();
            return;
        } else {
            UserModel userModel = new UserModel();
            Call call = userModel.updateuser(user_id, user_name, user_password,user_phone);
            call.enqueue(new Callback<User>() {
                @Override
                public void onResponse(Call<User> call, Response<User> response) {
                    User result = response.body();
                    String data = result.getSuccess();
                    System.out.print(data);
                    if ("1"==data) {
                        Toast.makeText(ModifyUserActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                        //Toast.makeText(MyInfoActivity.this, password, Toast.LENGTH_SHORT).show();
                        finish();

                    } else {
                        Toast.makeText(ModifyUserActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {

                }
            });
        }
    }
}
